package net.ib.mtalk.ui.widget;

import net.ib.mtalk.R;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.constant.MTalkConstant;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class MTalkActionItem extends ActionItem implements OnClickListener{

	public enum ItemType {
		ProfileView(0),
		OneOnOneChat(1),
		Call(2),
		/**
		 * Not supported on phase 1
		 */
		VideoChat(3),
		/**
		 * Not supported on phase 1
		 */
		PushToTalk(4);
		
		int type;
		ItemType(int i){
			type = i;
		}
		public int getType(){
			return type;
		}
	}

	private Context context;
	private ItemType itemType;
	private long sid = 0;
	private String[] arrPhone;
	private QuickAction parentQa;
	private Dialog parentDialog;
	
	public MTalkActionItem(Context _context, ItemType _itemType, long _sid, QuickAction _parentQa, Dialog dialog){
		this.parentDialog = dialog;
		this.sid = _sid;
		this.context = _context;
		this.itemType = _itemType;
		this.parentQa = _parentQa;
		setIcon();
		setOnClickListener();
	}
	
	public MTalkActionItem(Context _context, ItemType _itemType, long _sid, QuickAction _parentQa){
		this(_context, _itemType, _sid, _parentQa, null);
	}
	
	public MTalkActionItem(Context _context, ItemType _itemType){
		this(_context, _itemType, 0, null);
	}
	
	private void setIcon(){
		if(ItemType.ProfileView.equals(this.itemType)){
			super.setIcon(context.getResources().getDrawable(R.drawable.quick_contact_card));
		} else if(ItemType.OneOnOneChat.equals(this.itemType)){
			super.setIcon(context.getResources().getDrawable(R.drawable.quick_contact_im));
		} else if(ItemType.Call.equals(this.itemType)){
			super.setIcon(context.getResources().getDrawable(R.drawable.quick_contact_call));
		}
	}

	/**
	 * ItemType에 맞는 Intent 를 전송한다
	 * @throws Exception 
	 */
	private void setOnClickListener(){
		super.setOnClickListener(this);
	}
	
	/**
	 * 
	 * @param _sid
	 */
	public void setSid(long _sid){
		this.sid = _sid;
	}

	@Override
	public void onClick(View view) {
		view.setBackgroundResource(R.drawable.quickcontact_slider_btn_pressed);
		try{
			if(ItemType.ProfileView.equals(this.itemType)){
				if(this.sid==0) throw new Exception("YOU MUST SET SID!");
//				Intent intent = new Intent(context, BuddyProfile.class);
//				intent.putExtra(PersonTable.KEY_SID, this.sid);
//				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				context.startActivity(intent);
			} else if(ItemType.OneOnOneChat.equals(this.itemType)){
				if(this.sid==0) throw new Exception("YOU MUST SET SID!");
//				SMSServiceUtil.makeAndMoveOneChatting(context, sid, null);
			} else if(ItemType.Call.equals(this.itemType)){
				if(this.sid==0) throw new Exception("YOU MUST SET SID!");
//				ArrayList<String> phoneList = (ArrayList<String>) AttributeDAO.getPhoneNumbers(context, sid);
//				arrPhone = new String[phoneList.size()];
//				for(int i=0; i<phoneList.size(); i++) arrPhone[i] = phoneList.get(i);
//				
//				AlertDialog.Builder builder = new AlertDialog.Builder(context)
//					.setTitle(R.string.quick_action_call)
//					.setSingleChoiceItems(arrPhone, -1, new DialogInterface.OnClickListener() {
//						@Override
//						public void onClick(DialogInterface dialog, int index) {
//							Intent intent = new Intent(Intent.ACTION_CALL);
//							intent.setData(Uri.parse("tel:"+arrPhone[index]));
//							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//							context.startActivity(intent);
//							dialog.dismiss();
//						}
//					});
//				builder.create().show();
			}
		} catch(Exception e) {
			Log.e(MTalkConstant.TAG, "", e);
		} finally {
			if(parentDialog!=null) parentDialog.dismiss();
			if(parentQa!=null) parentQa.dismiss();
		}
	}

}
